import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.simplify.DouglasPeuckerLineSimplifier;


public class GpsModeler {
	
	 static GpsTrack generalizeDP(GpsTrack trackIn, double dmini) {
		CoordinateTime[] coord = Util.path2coordTime(trackIn);
		DouglasPeuckerLineSimplifier dpls = new DouglasPeuckerLineSimplifier(coord);
		dpls.setDistanceTolerance(dmini);
		Coordinate[] coord_gen = dpls.simplify();
		String name = trackIn.meta.name;
		String desc = trackIn.meta.desc;
		String author = trackIn.meta.author;
		String email = trackIn.meta.email;
		String keywords = trackIn.meta.keywords;
		GpsTrack trackOut = Util.coordTime2path(coord_gen, coord);
		trackOut.meta.author = author;
		trackOut.meta.email = email;
		trackOut.meta.keywords = keywords;

		double dmini_meter = dmini / Util.meter2angle();
		trackOut.addDesc("DP " + Util.format(dmini_meter, 2));
		trackOut.meta.name += "_g";
		return trackOut;
	}
	 
		static GpsTrack reduceToPlausibleSpeeds(GpsTrack trackIn, double vmax) {
			GpsTrack trackOut = trackIn.copy();
			double ts = trackOut.totalSpeed();
			if (vmax < ts) vmax = ts * 1.05;
			System.out.println("reduceToPlausibleSpeeds, max: " + ts + "; vmax: " + vmax);
			do {
				trackOut.doSpeeds();
				double speed_max = Double.MIN_VALUE;
				int position_max = -1;
				for (int i = 1; i < trackOut.trackPoints.size(); i++) {
					double speed_now = ((Location) trackOut.trackPoints.elementAt(i)).speed;
					if (speed_now > speed_max) {
						speed_max = speed_now;
						position_max = i;
					}
				}	
				if (speed_max < vmax) return trackOut;
				trackOut.trackPoints.remove(position_max);
				System.out.println(position_max);
//					System.out.println("remove " + position_max + " (" + speed_max + ")");
			} while (true);
		}

		static GpsTrack reduceToPlausibleSpeedsPause(GpsTrack trackIn, double vmin) {
			GpsTrack trackOut = trackIn.copy();
			double ts = trackOut.totalSpeed();
			if (vmin > ts) vmin = ts / 1.05;
			System.out.println("reduceToPlausibleSpeeds, min: " + ts + "; vmin: " + vmin);
			do {
				trackOut.doSpeeds();
				double speed_min = Double.MAX_VALUE;
				int position_min = -1;
				for (int i = 1; i < trackOut.trackPoints.size(); i++) {
					double speed_now = ((Location) trackOut.trackPoints.elementAt(i)).speed;
					if (speed_now < speed_min) {
						System.out.println(speed_now + " speedmin");
						speed_min = speed_now;
						position_min = i;
					}
				}	
				if (speed_min > vmin) return trackOut;
				trackOut.removePauseTime(position_min);
				trackOut.trackPoints.remove(position_min);
				System.out.println(position_min);
//					System.out.println("remove " + position_max + " (" + speed_max + ")");
			} while (true);
		}

}
